﻿using Game.NetWork;
using GameEntitySystem;
using MFTrain;

namespace Game
{
    public class ComponentMFEnginePackage : IPackage
    {
        public enum EventType
        {
            SyncStat,
            TurnToLowSen,
            TurnToHighSen,
            ZeroSen
        }

        public float Sen;

        public float rate;

        public float dt;

        public float ATKNow;

        public float force;

        public EventType m_eventType;

        public ushort entityId;

        public byte ID => 111;

        public Client To { get; set; }

        public Client Except { get; set; }

        public Client From { get; set; }

        public ClientState MinNeedState => ClientState.ProjectLoaded;

        public ComponentMFEnginePackage()
        {
        }

        public ComponentMFEnginePackage(ComponentMFEngine flu, EventType eventType)
        {
            entityId = flu.Entity.EntityId;
            m_eventType = eventType;
            force = flu.Work;
        }

        public void WriteData(PackageStreamWriter writer)
        {
            writer.Write(entityId);
            writer.Write(force);
            writer.WriteEnum(m_eventType);
        }

        public void ReadData(PackageStreamReader reader)
        {
            entityId = reader.ReadUInt16();
            m_eventType = reader.ReadEnum<EventType>();
            force = reader.ReadSingle();
        }

        public void Handle(ProjectNet projectNet, NetNode netNode, bool isServer)
        {
            if (m_eventType != 0)
            {
                return;
            }

            projectNet.FindEntityById(entityId, delegate (Entity entity)
            {
                ComponentMFEngine componentMFEngine = entity.FindComponent<ComponentMFEngine>();
                if (componentMFEngine != null)
                {
                    componentMFEngine.Work = force;
                }
            });
        }
    }
}